package com.dubbo.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.server.service.map.RecommendUserServiceMapp;
import com.dubbo.server.vo.TodayBest;
import com.tanhua.common.pojo.PageInfo;
import com.tanhua.common.pojo.RecommendUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Service(timeout = 60000)
public class RecommendUserService implements RecommendUserServiceMapp {


    @Autowired
    private MongoTemplate mongoTemplate;


    @Autowired
    private RecommendUserApiImpl recommendUserApi;


    @Autowired
    private UserInfoService userInfoService;

    /**
     * @param userId 今日佳人功能实现
     * @return todayBest 推荐的人的ID  推荐的人和自己的缘分值
     */
    public TodayBest queryTodayBest(Long userId) {
        // recommendUserApi Dubbo的代理对象
//        @param userId 登录人的ID
        RecommendUser recommendUser = mongoTemplate.findOne(Query.query(Criteria.where("toUserId").is(userId))
                        .with(Sort.by(Sort.Order.desc("scoer"))).limit(1)
                , RecommendUser.class);

        if (null == recommendUser) {

            return null;
        }
        // VO: View Object APP视图  展示数据的
        TodayBest todayBest = new TodayBest();
        // 佳人id
        todayBest.setId(recommendUser.getUserId());
        //缘分值  向下取整Math.floor(recommendUser.getScore());//取整,98.2 -> 98
        todayBest.setFateValue(Double.valueOf(Math.floor(recommendUser.getScore())).longValue());
        return todayBest;
    }


    /**
     * 首页推荐列表功能接口实现
     * desc 降序   asc升序
     *
     * @param userId   登录人的ID
     * @param page     查询的页码
     * @param pageSize 查询的条数
     * @return
     */
    public PageInfo<RecommendUser> queryRecommendUserList(Long userId, Integer page, Integer pageSize) {
        //从零开始(查询不到数据必须问)
        List<RecommendUser> recommendUserList = mongoTemplate.find(Query.query(Criteria.where("toUserId").is(userId)).
                        with(PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("score")))),
                RecommendUser.class);
        //没有意义需要改1111
        return new PageInfo<>(0, page, pageSize, recommendUserList);
    }


    /**
     * 查询推荐好友的缘分值
     *
     * @param userId
     * @param toUserId
     * @return
     */
    @Override
    public Double queryScore(Long userId, Long toUserId) {
        Double score = recommendUserApi.queryScore(userId, toUserId);
        if (ObjectUtil.isNotEmpty(score)) {
            return score;
        }
        //默认值
        return 98d;
    }


    /**
     * 查询探花卡片列表
     *
     * @param id
     * @param count
     * @return
     */
    @Override
    public List<RecommendUser> queryCardList(Long id, int count) {
        return this.recommendUserApi.queryCardList(id, count);
    }
}
